package com.cygnus.controller;

import com.cygnus.model.Employee;
import com.cygnus.model.Msg;
import com.cygnus.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
处理员工CRUD请求(分页查询)
 */
@Controller
public class EmployeeControllerOld {
    @Autowired
    EmployeeService employeeService;
//restful风格的请求 相同的请求头 不同的请求参数和请求方法 导致不同的请求结果
    @RequestMapping(value = "/emps",method = RequestMethod.GET)
    public String getEmps(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,Model model){
//        要实现分页，首先需要调用PageHelper.startPage，传入页码pn和每一页的记录数pageSize
        PageHelper.startPage(pageNum,5);
//        而后startPage下一句的查询就会是一个分页查询
        List<Employee> employees = employeeService.getAll();

/*        还可以使用pageinfo包装查询结果，将其中封装的详细分页信息交给页面:
            navigatePages:导航栏页数
 */
/*      employees会被保存在PageInfo的父类PageSerializable的属性 protected List<T> list 中
        要得到employees，通过getList()。绝非PageInfo.employees
 */
        PageInfo employeePageInfo = new PageInfo(employees,5);
        model.addAttribute("pageInfo",employeePageInfo);
        employeePageInfo.isHasPreviousPage();

        return "list";
    }

    @ResponseBody
    @RequestMapping(value = "/emps" ,method = RequestMethod.POST)
    public Msg saveEmp(Employee employee){
        employeeService.saveEmp(employee);
        return Msg.success();
    }
}
